<template>
  <div class='tags'>
    <el-card style="margin-bottom:20px">
      <!-- 面包屑组件 -->
      <div slot="header" v-if="this.$route.query.id">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{this.$route.query.name}}</el-breadcrumb-item>
          <el-breadcrumb-item>标签管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 表单搜索查询区域 -->
      <div style="float: left">
        <el-form ref="form" :inline="true" :model="form" class="demo-form-inline">
          <el-form-item label="目录名称">
            <el-input v-model="form.tagName"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="form.state" placeholder="请选择">
              <el-option label="开启" value="1"></el-option>
              <el-option label="屏蔽" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="clear">清除</el-button>
            <el-button type="primary" @click="search">搜索</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- 新增目录标签 -->
      <div style="float: right">
        <el-button type="text" icon="el-icon-back" v-if="this.$route.query.id" @click="tosubjects()">返回学科</el-button>
        <el-button type="success" icon="el-icon-edit" @click="addTag()">新增目录</el-button>
      </div>
      <!-- 新增目录对话框 -->
      <addtags
      ref="formInline"
      :formInline="formInline"
      :tablevisible="dialogVisible"
      @close="close"
      @getNew="getTags"></addtags>
      <!--消息提示的文案  -->
      <el-alert
      style="margin-bottom:5px"
        :closable="false"
        :title="`数据一共${this.counts}条`"
        type="info"
        show-icon>
      </el-alert>
      <!-- 标签列表（表格） -->
        <el-table
          v-loading="loading"
          element-loading-text="拼命加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.8)"
          :data="items"
          style="width: 100%">
          <el-table-column
            prop="id"
            label="序号">
          </el-table-column>
          <el-table-column
            prop="subjectName"
            label="所属学科"
            width="180">
          </el-table-column>
          <el-table-column
            prop="tagName"
            label="标签名称">
          </el-table-column>
           <el-table-column
            prop="creatorID"
            label="创建者">
          </el-table-column>
          <el-table-column
            prop="addDate"
            label="创建日期"
            width="180">
            <template slot-scope="scope">
              {{scope.row.addDate | parseTimeByString}}
            </template>
          </el-table-column>
          <el-table-column
            label="状态"
            width="180">
            <template slot-scope="scope">
             {{scope.row.state === 0 ? '已关闭' : '已开启'}}
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="180">
            <template slot-scope="scope">
              <el-button type="text" @click="changeState(scope.row, scope.$index)" :loading="scope.$index === idx && isopen">
                {{scope.row.state === 0 ? '启用' : '禁用'}}
              </el-button>
              <el-button type="text" :disabled="scope.row.state === 0 ? false : true" @click="addTag(scope.row)">修改</el-button>
              <el-button type="text" :disabled="scope.row.state === 0 ? false : true" @click="del(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 底部分页部分 -->
        <el-footer style="padding-top:20px;padding-bottom:20px;float:right">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="requestparams.page"
            :page-sizes="[6, 10]"
            :page-size="6"
            layout="total, sizes, prev, pager, next, jumper"
            :total="counts">
          </el-pagination>
        </el-footer>
    </el-card>
  </div>
</template>

<script>
import addtags from '../../module-hmmm/components/tags-add.vue'
import { list, remove, changeState } from '@/api/hmmm/tags.js'
export default {
  name: 'tags',
  components: {
    addtags
  },
  data () {
    return {
      isopen: false,
      idx: -1,
      loading: true,
      form: {
        tagName: null,
        state: null
      },
      dialogVisible: false,
      requestparams: {
        subjectID: null,
        page: 1,
        pagesize: 6,
        state: null,
        tagName: null,
        subjectName: null
      },
      formInline: {},
      // 总记录数
      counts: 0,
      // 每页大小/当前页码、总页数
      items: []
    }
  },
  created () {
    this.getTags()
  },
  methods: {
    // 返回学科列表
    tosubjects () {
      this.$router.push('list')
    },
    // 关闭对话框
    close () {
      this.dialogVisible = false
    },
    // 分页（分页条数变化）
    handleSizeChange (pagesize) {
      this.requestparams.pagesize = pagesize
      this.getTags()
    },
    // 当前页改变
    handleCurrentChange (currentPage) {
      this.requestparams.page = currentPage
      this.getTags()
    },
    // 删除标签
    del (item) {
      /*  const res = remove(item) */
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(item)
        // 删除完成后刷新页面
        this.getTags()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 切换操作按钮状态
    async changeState (row, index) {
      // console.log(row.id)
      this.idx = index
      this.isopen = true
      // index.loading = true
      // this.isopen = true
      const data = {
        id: row.id,
        state: row.state === 0 ? 1 : 0
      }
      await changeState(data)
      row.state = row.state === 0 ? 1 : 0
      this.$message.success('操作成功')
      this.isopen = false
    },
    // 新增目录
    addTag (data) {
      // console.log(data)
      if (data) {
        this.formInline = data
      } else {
        if (this.$route.query.id) {
          this.formInline.subjectID = parseInt(this.$route.query.id)
        } else {
          this.formInline = {}
        }
      }
      this.dialogVisible = true
    },
    // 点击搜索查询
    search () {
      // 获取表单数据
      // console.log(this.formInline)
      this.requestparams.tagName = this.form.tagName
      this.requestparams.state = this.form.state
      // 发送请求
      this.getTags(this.requestparams)
    },
    // 清空表单
    clear () {
      this.form = {}
      this.search()
    },
    // 获取标签列表
    async getTags () {
      // 显示loading
      // this.isLoading = true
      // 查询条件
      this.requestparams.subjectID = this.$route.query.id || null
      this.requestparams.subjectName = this.$route.query.name || null
      const { data: res } = await list(this.requestparams)
      // console.log(res)
      this.items = res.items
      this.counts = res.counts
      this.pagesize = res.pagesize
      // 关闭loading
      this.loading = false
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
